package dao;

import domain.Permission;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author Freedy
 * @date 2020/12/25 22:21
 */
public interface IPermissionDao {

    /**
     * 根据roleID查询所有Permission
     * @param roleId
     * @return
     * @throws Exception
     */
    @Select("select * from permission where id in (select permissionId from role_permission " +
            "where roleId=#{roleId})")
    List<Permission> findPermissionByRoleId(String roleId) throws Exception;

    /**
     * 查询所有
     * @return
     */
    @Select("select * from permission")
    List<Permission> findAll();

    /**
     * 保存
     * @param permission
     */
    @Insert("insert into permission(permissionName,url) values(#{permissionName},#{url})")
    void save(Permission permission);

    /**
     * 删除中间表中的权限
     * @param permissionId
     * @throws Exception
     */
    @Delete("delete from role_permission where permissionId=#{permissionId}")
    void deletePermissionFromRolePermission(String permissionId) throws Exception;

    /**
     * 根据id删除权限
     * @param permissionId
     * @throws Exception
     */
    @Delete("delete from permission where id=#{permissionId}")
    void deletePermission(String permissionId) throws Exception;

    /**
     * 根据id查询
     * @param permissionId
     * @return
     * @throws Exception
     */
    @Select("select * from permission where id =#{permissionId}")
    Permission findById(String permissionId) throws Exception;
}
